package org.mtmi.ui.drawables;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.mtmi.ui.scenes.CLR;
import org.mtmi.ui.scenes.COLOR;

/**
 * Draw a simple symbol sign in a square, see list in constant below.
 * 
 */
public class SymbolDrawable implements IDrawable
{
	/** the '+' sign */
	public static final int PLUS=0;
	/** the 'X' sign */
	public static final int CROSS=1;
	/** the 'v' check sign */
	public static final int CHECK=3;
	/** leftward arrow */
	public static final int ARROW_LEFT=4;
	/** rightward arrow */
	public static final int ARROW_RIGHT=5;
	/** topward arrow */
	public static final int ARROW_TOP=6;
	/** bottomward arrow */
	public static final int ARROW_BOTTOM=7;

	private Color color, disabled;
	private int   sign;
	
	/** caller is responsible to dispose color */
	public SymbolDrawable( Color clr, int _sign ) {
		color = clr;
		sign = _sign;
		disabled = COLOR.grey50;
	}
	
	public Color getColor() { return color; }
	
	/** caller is responsible to dispose color */
	public void setColor( Color c ) { color=c; }
	
	
	public Color getDisabledColor() { return disabled; }
	
	/** caller is responsible to dispose color */
	public void setDisabledColor( Color c ) { disabled=c; }
	
	/** caller is responsible to dispose color */
	public void setColors( Color c, Color _disabled ) {
		color = c;
		disabled = _disabled;
	}
	
	@Override
	public Point computeSize( int width, int height ) {
		int r = Math.min( width, height );
		if( r < 24 ) r=24;
		return new Point( r, r );
	}

	@Override
	public void paint( GC gc, int x, int y, int width, int height, int state ) {
		
		int l = Math.min( width, height );
		if( l > 6 ) {
			
			int x1 = x + (width-l)/2;
			int y1 = y + (height-l)/2;
			gc.setBackground( color );
			int xy[]=null;
			
			switch( sign ) {
			case PLUS: {
				int x2 = x1+l/3, x3=x1+2*l/3, x4=x1+l;
				int y2 = y1+l/3, y3=y1+2*l/3, y4=y1+l;
				xy = new int[] {
                   x2, y1, x3, y1, x3, y2, x4, y2,
                   x4, y3, x3, y3, x3, y4, x2, y4,
                   x2, y3, x1, y3, x1, y2, x2, y2
				};
				break; }
			
			case CROSS:{
				int l1 = l/4, l2=l/2, l3=3*l/4;
				xy = new int[] {
					x1+l1, y1,    x1+l2, y1+l1,  x1+l3, y1,
					x1+l, y1+l1,  x1+l3, y1+l2,  x1+l,  y1+l3,
					x1+l3, y1+l,  x1+l2, y1+l3,  x1+l1, y1+l,
					x1, y1+l3,    x1+l1, y1+l2,  x1, y1+l1
				};
				break;}
			case CHECK:{
				int l1 = l/5, l2=2*l1, l4=4*l1, l5=5*l1;
				y1 += l1;
				xy = new int[] {
					x1, y1+l2,   x1+l1, y1+l1,  x1+l2, y1+l2,
					x1+l4, y1,   x1+l5, y1+l1,  x1+l2, y1+l4,
				};
				break; }
			case ARROW_LEFT:{
				xy = new int[] { x1,y1+l/2, x1+l,y1, x1+l,y1+l };
				break;}
			case ARROW_RIGHT:{
				xy = new int[] { x1,y1, x1+l,y1+l/2, x1,y1+l };
				break;}
			case ARROW_TOP:{
				xy = new int[] { x1+l/2,y1, x1+l,y1+l, x1,y1+l };
				break;}
			case ARROW_BOTTOM:{
				xy = new int[] { x1,y1, x1+l,y1, x1+l/2,y1+l };
				break;}
			default: throw new Error("unhandled symbol: "+sign); //$NON-NLS-1$
			}
		
			/*if( state==PRESSED ) {
				for( int i=0; i<xy.length; ) {
					xy[i++]++;
					xy[i++]++;
				}
			}*/
			Color b = null;
			Color c = null;
			
			Color primary = state==DISABLED ? disabled : color ;
			
			if( state == PRESSED ) {
				b = CLR.lighter( primary, 0.7f );
				gc.setBackground( b );
				c = CLR.darker( primary, 0.2f );
				gc.setForeground( c );
			} else {
				gc.setBackground( primary );
				c = CLR.darker( primary, 0.7f );
				gc.setForeground( c );
			}
			gc.fillPolygon( xy );
			gc.setLineWidth( 2 );
			gc.drawPolygon( xy );
			if( b!=null ) b.dispose();
			if( c!=null ) c.dispose();
		}
	}
}
